package com.inputstream_;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author Vhong
 * @version 1.0
 */
public class FileInputStream_ {
    public static void main(String[] args) {

    }
    @Test
    public void readFile01(){
        String filePath = "e:\\hello.txt";
        int readData = 0;
        //创建FileInputStream对象，用于读取文件
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(filePath);
            //从该输入流读取一个字节的数据。如果没有输入可用，此方法将阻止
            //如何返回-1，表示读取完毕
            while((readData = fileInputStream.read()) != -1 ){
                System.out.print((char)readData);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                //关闭文件流，释放资源
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @Test
    public void readFile02(){
        String filePath = "e:\\hello.txt";
        int readLen = 0;
        byte[] buf = new byte[8];
        //创建FileInputStream对象，用于读取文件
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(filePath);
            //从该输入流读取一个字节的数据。如果没有输入可用，此方法将阻止
            //如何返回-1，表示读取完毕
            //如果读取正常，返回实际读取的字节数
            while((readLen = fileInputStream.read(buf)) != -1 ){
                System.out.print(new String(buf,0,readLen));//显示
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                //关闭文件流，释放资源
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
